import {
  getYongYouSaleWarehouseOutSet,
  saveYongYouSaleWarehouseOutSet,
  saveYongYouSaleWarehouseOutSetAndSync,
} from '@/api/application'
import { ref } from 'vue'

/**
 * 获取用友销售同步设置
 */
export const useGetSaleWarehouseOutSet = () => {
  // 销售同步设置
  const set = ref({
    saleStoreBillEndDate: '',
    saleStoreBillStartDate: '',
  })

  // 获取设置加载
  const getSetLoading = ref(false)

  /**
   * 获取设置
   */
  const getSet = async () => {
    getSetLoading.value = true
    const res = await getYongYouSaleWarehouseOutSet()
    set.value = res.data.data.row
    getSetLoading.value = false
  }

  return {
    set,
    getSetLoading,
    getSet,
  }
}

/**
 * 保存用友销售出库设置
 */
export const useSaveYongYouSaleWarehouseOutSet = () => {
  // 保存设置加载
  const saveSetLoading = ref(false)

  // 销售出库设置对象
  const saveSetVal = ref({
    saleStoreBillEndDate: '',
    saleStoreBillStartDate: '',
  })

  /**
   * 保存设置
   */
  const saveSet = async () => {
    saveSetLoading.value = true
    const res = await saveYongYouSaleWarehouseOutSet(saveSetVal.value)
    saveSetLoading.value = false
    return res.data
  }

  return {
    saveSetVal,
    saveSetLoading,
    saveSet,
  }
}

/**
 * 保存用友销售出库设置并同步
 */
export const useSaveYongYouSaleWarehouseOutSetAndAsync = () => {
  // 保存设置并开始同步加载
  const saveSetAndAsyncLoading = ref(false)

  // 设置对象
  const saveSetAndAsyncVal = ref({
    saleStoreBillEndDate: '',
    saleStoreBillStartDate: '',
  })

  /**
   * 保存设置并开始同步
   * @param {Number} uuid - 进度唯一标识
   */
  const saveSetAndAsync = async (uuid: number) => {
    saveSetAndAsyncLoading.value = true
    const res = await saveYongYouSaleWarehouseOutSetAndSync(saveSetAndAsyncVal.value, uuid)
    saveSetAndAsyncLoading.value = false
    return res.data
  }

  return {
    saveSetAndAsyncVal,
    saveSetAndAsyncLoading,
    saveSetAndAsync,
  }
}
